<!-- 维修评价编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="feedbackModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="feedbackModalApp.feedback"
        :rules="feedbackModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">
          <a-col :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-item label="反馈类型:" name="feedBackType">
              <m-dict-select
                v-model:value="feedbackModalApp.feedback.feedBackType"
                placeholder="请选择反馈类型"
                dict="反馈类型"/>
            </a-form-item>
          </a-col>
          <a-col :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-item label="内容:" name="content">
              <a-input
                v-model:value="feedbackModalApp.feedback.content"
                placeholder="请输入内容"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-item label="联系人:" name="contacts">
              <a-input
                v-model:value="feedbackModalApp.feedback.contacts"
                placeholder="请输入联系人"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-item label="联系方式:" name="contactMethod">
              <a-input
                v-model:value="feedbackModalApp.feedback.contactMethod"
                placeholder="请输入联系方式"
                :maxlength="255"
                allow-clear
              />
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="feedbackModalApp.display">
                <teleport to="#feedbackEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="feedbackListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(feedbackModalApp.feedback)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!feedbackModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(feedbackModalApp.feedback)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {FeedbackService} from "@/views/repair/repairManagement/feedback/feedbackService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MDictSelect from "@/components/MDict/dictSelect";

export default defineComponent({
  components: {
    MDictSelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let feedbackListApp = inject('feedbackListApp', reactive({}));
    let feedbackGrid = inject('feedbackGrid', reactive({}))
    const feedbackModalApp = reactive({
      id: null,
      feedback: {},
      isEdit: false,
      display: false
    });
    feedbackModalApp.rules = {
      feedBackType: [
        {
          required: true,
          type: 'string',
          message: '请输入反馈类型',
          trigger: 'blur'
        }
      ],
      content: [
        {
          required: true,
          type: 'string',
          message: '请输入内容',
          trigger: 'blur'
        }
      ],
      contacts: [
        {
          required: true,
          type: 'string',
          message: '请输入联系人',
          trigger: 'blur'
        }
      ],
      contactMethod: [
        {
          required: true,
          type: 'string',
          message: '请输入联系方式',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      feedbackModalApp.feedback = reactive({});
      if (routeId) {//路由
        feedbackModalApp.id = routeId;
        feedbackModalApp.display = false;
        findFeedback(feedbackModalApp.id);
      } else if (feedbackListApp.editModalForEdit) {//修改弹窗
        feedbackModalApp.id = feedbackListApp.currentId;
        feedbackModalApp.display = true;
      } else if (!feedbackListApp.editModalForEdit && feedbackListApp.addModalForEdit) {//新增弹窗
        feedbackModalApp.display = true;
      } else {
        feedbackModalApp.title = "新增";
      }
      if (feedbackModalApp.id) findFeedback(feedbackModalApp.id);
    })

    const findFeedback = (id) => {
      FeedbackService.findFeedbackForView(id).then((res) => {
        feedbackModalApp.feedback = res.data;
        //TODO:为编辑准备.
        // feedbackModalApp.title = "编辑学生 " + feedbackModalApp.feedback.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        FeedbackService.updateFeedback(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        FeedbackService.saveFeedback(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/repair/repairManagement/feedback`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        feedbackGrid.value.commitProxy("query");
      }, 1500);
      feedbackListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      feedbackListApp,
      feedbackModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
